// pages/goodslist/goodslist.js
const app = getApp();

const api = require('../../utils/api.js')
const { myRequest } = require('../../utils/myRequest.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    size: 10,
    categoryId: '',
    goodsList:'',
    brotherCategory:'',
    parentCategory:'',
    totalPages:''
  },

  // 点击 跳转到商品详情页
  toGoodsDet(e){
    let id = e.currentTarget.dataset.id;
    console.log(id);
    wx.navigateTo({
      url: `../../Paypages/pages/good_detail/good_detail?goodsId=${id}`,
    })
  },

  // 点击导航栏相应获取商品列表再渲染
  isgoindex(e){
    let{categoryId,goodsList,page,size}=this.data
    console.log(e.target.id)
    categoryId=e.target.id
    page=1
    this.setData({
      categoryId:categoryId,
      page:page
    })
    let that = this;
    // 请求
    myRequest({
      url: api.goodsList,
      data:
      {
        categoryId: categoryId,
        page: page,
        size: size
      },
      success(res) {
        console.log('商品列表', res.data.data)
        goodsList = res.data.data.goodsList;
        that.setData({
          goodsList: goodsList,
        })
      }
    })
  },
  // 滚动到一定底部获取下一页的数据
  updata(e){
    let { page, totalPages,categoryId,goodsList,size} =this.data
    let resArr=[];      //声明一个空数据保存

    // 若当前页数小于总页数，则不再+1
    if (page < totalPages){
      page+=1;
      this.setData({
        page:page
      })
      let that = this;        //保存this的指向
      // 请求
      myRequest({
        url: api.goodsList,
        data:
        {
          categoryId: categoryId,
          page: page,
          size: size
        },
        success(res) {
          // 遍历push到新声明的变量
          for (var i = 0;i <res.data.data.goodsList.length;i++)
            resArr.push(res.data.data.goodsList[i]);

          console.log(resArr)
  
          var cont = goodsList.concat(resArr);     //将当前页的数据与下一页的数据拼接起来,并赋值于一个变量
          console.log(cont)
          that.setData({
            goodsList: cont,
          })
        }
      })
    } else if (page = totalPages){
      wx.showToast({
        title: '我是有底线的!!!',
        icon:'none',
        duration:1000
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

      console.log("接受到的参数",options)

    let { categoryId, page, size, goodsList, brotherCategory, parentCategory, totalPages } = this.data
        categoryId=options.categoryId
    this.setData({
      categoryId:categoryId
    })
    let that = this;
    
    // 请求分类数据(带上商品id)
    myRequest({
      url: api.goodsCategory,
      data: {
        id: categoryId,
      },
      success(res) {
        console.log("分类数据", res.data.data)
        brotherCategory = res.data.data.brotherCategory
        parentCategory = res.data.data.parentCategory
        that.setData({
          brotherCategory: brotherCategory,
          parentCategory: parentCategory
        })

        // 设置导航栏标题
        wx.setNavigationBarTitle({
          title: parentCategory.name
        })
      }
    })
    
    // 结合2接口获取商品列表
    myRequest({
      url: api.goodsList,
      data:
      {
        categoryId: categoryId,
        page: page,
        size: size
      },
      success(res) {
        console.log('商品列表', res.data.data)
        goodsList=res.data.data.goodsList;
        totalPages = res.data.data.totalPages
        that.setData({
          goodsList:goodsList,
          totalPages: totalPages
        })
      }
    })


  },
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})